package com.yunshan.reserve;

import com.yunshan.common.annotation.RepeatSubmit;
import com.yunshan.common.core.controller.BaseController;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.utils.UserUtils;
import com.yunshan.order.domain.OrderReserveNotice;
import com.yunshan.order.domain.ReserveVo;
import com.yunshan.order.service.IOrderReserveNoticeService;
import com.yunshan.wechat.domain.WechatUserInfo;
import com.yunshan.wechat.service.IWechatUserInfoService;
import com.yunshan.welfare.domain.WelfareCouponRecord;
import com.yunshan.welfare.domain.WelfareGiftRecord;
import com.yunshan.welfare.service.IWelfareCouponRecordService;
import com.yunshan.welfare.service.IWelfareGiftRecordService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.yunshan.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 预约Controller
 *
 * @author yunshan
 * @date 2025-02-12
 */
@Api(tags = "预约")
@RestController
@RequestMapping("/wx/reserve")
public class ReserveController extends BaseController
{
    @Autowired
    private IOrderReserveNoticeService orderReserveNoticeService;
    @Autowired
    private IWelfareCouponRecordService welfareCouponRecordService;
    @Autowired
    private IWelfareGiftRecordService welfareGiftRecordService;


    /**
     * 活动预约
     */
    @ApiOperation("活动预约")
    @RepeatSubmit
    @PostMapping("/activity")
    public AjaxResult activity(
            @ApiParam(name = "activityReserveVo", value = "活动预约对象", required = true)
            @RequestBody ReserveVo activityReserveVo)
    {
        try {
            return success(orderReserveNoticeService.insertActivityReserve(activityReserveVo));
        }catch (Exception e){
            log.error("活动预约失败: {}", e.getMessage());
            return AjaxResult.error("活动预约失败");
        }
    }

    /**
     * 服务预约
     */
    @ApiOperation("服务预约")
    @RepeatSubmit
    @PostMapping("/serve")
    public AjaxResult serve(
            @ApiParam(name = "activityReserveVo", value = "服务预约对象", required = true)
            @RequestBody ReserveVo activityReserveVo)
    {
        try {
            return success(orderReserveNoticeService.insertServeReserve(activityReserveVo));
        }catch (Exception e){
            log.error("服务预约失败: {}", e.getMessage());
            return error(e.getMessage());
        }
    }

    @ApiOperation("用户预约管理")
    @GetMapping("/reserve")
    private AjaxResult getReserve(
            @ApiParam(name = "status", value = "使用状态 0:待使用  1:已核销  2：已取消 ", required = true)
            @RequestParam Integer status)
    {
        OrderReserveNotice orderReserveNotice = new OrderReserveNotice();
        orderReserveNotice.setStatus(status);
        return success(orderReserveNoticeService.selectUniReserveNotice(orderReserveNotice));
    }

    @ApiOperation("用户预约详情")
    @GetMapping("/reserveInfo")
    private AjaxResult getReserveInfo(
            @ApiParam(name = "id", value = "预约id ", required = true)
            @RequestParam Long id)
    {
        return success(orderReserveNoticeService.selectUniReserveNoticeInfo(id));
    }

    @ApiOperation("查看券码")
    @GetMapping("/reserveCode")
    private AjaxResult getReserveCode(
            @ApiParam(name = "id", value = "预约id ", required = true)
            @RequestParam Long id)
    {
        return success(orderReserveNoticeService.selectOrderReserveNoticeById(id).getCode());
    }

    @ApiOperation("取消预约")
    @RepeatSubmit
    @PostMapping("/cancel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "预约id", required = true),
            @ApiImplicitParam(name = "cancelReason", value = "取消原因", required = true),
            @ApiImplicitParam(name = "cancelDescription", value = "取消说明"),
    })
    private AjaxResult cancel(@RequestBody OrderReserveNotice orderReserveNotice)
    {
        OrderReserveNotice orderReserve = orderReserveNoticeService.selectOrderReserveNoticeById(orderReserveNotice.getId());
        if (orderReserve == null){
            return error("预约服务异常");
        }
        if (orderReserve.getStatus() == 1) {
            return error("预约服务已核销");
        }

        //优惠券则更新回未使用的状态
        if (orderReserve.getPayType() == 3){
            WelfareCouponRecord welfareCouponRecord = new WelfareCouponRecord();
            welfareCouponRecord.setId(orderReserve.getServeId());
            WelfareCouponRecord welfareCoupon = welfareCouponRecordService.selectWelfareCouponRecordById(welfareCouponRecord.getId());

            //判断优惠券是否过期
            if (welfareCoupon != null && welfareCoupon.getEndTime().before(new Date())) {
                welfareCouponRecord.setStatus(2);
            }else {
                welfareCouponRecord.setStatus(0);
            }

            welfareCouponRecordService.updateWelfareCouponRecord(welfareCouponRecord);
        }else if ((orderReserve.getPayType() == 2)){
            //查询礼包
            WelfareGiftRecord welfareGiftRecord = new WelfareGiftRecord();
            welfareGiftRecord.setGiftContentId(orderReserve.getServeId());
            welfareGiftRecord.setStatus(1);
            List<WelfareGiftRecord> welfareGiftRecords = welfareGiftRecordService.selectWelfareGiftRecordList(welfareGiftRecord);

            //判断礼包优惠券是否过期
            if (welfareGiftRecords.size() > 0){
                WelfareGiftRecord giftRecord = welfareGiftRecords.get(0);
                if (giftRecord.getEndTime().before(new Date())) {
                    giftRecord.setStatus(2);
                }else {
                    giftRecord.setStatus(0);
                }

                welfareGiftRecordService.updateWelfareGiftRecord(giftRecord);
            }else {
                return error("礼包优惠券异常，取消失败");
            }
        }

        orderReserveNotice.setStatus(2);
        return success(orderReserveNoticeService.updateOrderReserveNotice(orderReserveNotice));
    }

}
